﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolWeb.Models;
using Microsoft.Web.Helpers;

namespace GameSchoolWeb.Controllers
{
	[Authorize]
	public class VideoController : Controller
	{
		ICourseRepository m_repository;

		public VideoController()
		{
			m_repository = new CourseRepository();
		}

		public VideoController(ICourseRepository rep)
		{
			m_repository = rep;
		}

		public ActionResult Index()
		{
			return View();
		}

		[HttpGet]
		public ActionResult CreateLecture()
		{
			return View(new Lecture());
		}

		[HttpPost]
		public ActionResult CreateLecture(FormCollection formdata, int? id)
		{
			if (id == null) return View("NotFound");
			Lecture lectureItem = new Lecture();
			UpdateModel(lectureItem);
			lectureItem.dDateAdded = DateTime.Now;
			lectureItem.levelId = id.Value;
			// LectureItem er nýr fyrirlestur í núverandi borð
			if (lectureItem != null)
			{
				m_repository.AddLecture(lectureItem);
				m_repository.Save();

				return RedirectToAction("GetLectureListByLevelId", new { id = id.Value });
			}

			return View("NotFound");
		}

		[HttpGet]
		public ActionResult GetLectureListByLevelId(int? id)
		{
			if (id == null) return View("NotFound");
			// lectureItems er listi af fyrirlestrum í núverandi borði.
			var lectureItems = from l in m_repository.Lecture()
							   where l.levelId == id
							   orderby l.dDateAdded ascending
							   select l;

			int lvlId = id.Value;

			if (lectureItems == null)
			{
				return View("NotFound");
			}

			LectureListModelView vm = new LectureListModelView(lectureItems, lvlId);
			return View(vm);
		}

		[HttpGet]
		public ActionResult GetLecture(int? id)
		{
			if (id == null) return View("NotFound");
			// lectureItem er sá fyrirlestur sem valinn var.
			var lectureItem = (from c in m_repository.Lecture()
							   where c.id == id
							   select c).SingleOrDefault();

			if (lectureItem != null)
			{
				return View(lectureItem);
			}
			return View("NotFound");
		}
	}
}
